﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BitTorrentSharp.Maths;

namespace BitTorrentSharp.Test
{
    /// <summary>
    /// MathsUnitTest 的摘要说明
    /// </summary>
    [TestClass]
    public class MathsUnitTest
    {
        public MathsUnitTest()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，该上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        //
        // 编写测试时，可以使用以下附加特性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前，使用 TestInitialize 来运行代码
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // 在每个测试运行完之后，使用 TestCleanup 来运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestBigInteger()
        {
            // test new operation and basic numbers

            BigInteger bigInt1 = new BigInteger(new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });

            Assert.AreEqual<bool>(true, bigInt1.Equals(UInt64.MaxValue));

            BigInteger bigInt2 = new BigInteger(new byte[] { 0 });

            Assert.AreEqual<bool>(true, bigInt2.Equals(UInt64.MinValue));

            BigInteger bigInt3 = new BigInteger(new byte[] { 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0 });

            Assert.AreEqual<bool>(true, bigInt3.Equals((UInt64)UInt32.MaxValue));

            BigInteger bigInt4 = new BigInteger(new byte[] { 0xff, 0xff });

            Assert.AreEqual<bool>(true, bigInt4.Equals((UInt64)UInt16.MaxValue));

            BigInteger bigInt5 = new BigInteger(new byte[] { 0xff });

            Assert.AreEqual<bool>(true, bigInt5.Equals((UInt64)byte.MaxValue));

            // test compare

            BigInteger bigInt6 = new BigInteger(new byte[] { 0xff });

            Assert.AreEqual<int>(-1, bigInt6.CompareTo((UInt64)0xfff));

            BigInteger bigInt7 = new BigInteger(new byte[] { 0xff });

            Assert.AreEqual<BigInteger>(bigInt6, bigInt7);

            BigInteger bigInt8 = new BigInteger(new byte[] { 0xff, 0xf });

            Assert.AreEqual<int>(1, bigInt8.CompareTo(bigInt7));

            // test dividedby 2

            BigInteger bigInt9 = new BigInteger(new byte[] { 0, 1 });

            Assert.AreEqual<BigInteger>(bigInt9.DividedBy2(), new BigInteger(new byte[] { 0x80 }));

            BigInteger bigInt10 = new BigInteger(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 });

            Assert.AreEqual<BigInteger>(bigInt10.DividedBy2(), new BigInteger(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0x80, 0x80, 0, 0, 0 }));

            BigInteger bigInt11 = new BigInteger(new byte[] { 3, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 3 });

            Assert.AreEqual<BigInteger>(bigInt11.DividedBy2(), new BigInteger(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0x80, 0x80, 0, 0x80, 1 }));
        }
    }
}
